//
//  TutorialViewController.swift
//  miaoyin
//
//  Created by David on 2017/2/28.
//  Copyright © 2017年 David. All rights reserved.
//

import UIKit

class TutorialViewController: UIViewController {

    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .lightContent
    }
    
    var imageView: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        modalTransitionStyle = .crossDissolve
        
        let bgView = UIVisualEffectView(frame: view.frame)
        bgView.effect = UIBlurEffect(style: .dark)
        view.addSubview(bgView)
        
        
        let btnsView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width*0.5, height: view.frame.width*0.5))
        btnsView.center = view.center
        bgView.addSubview(btnsView)
        
        let btnClose = UIButton(frame: CGRect(x: 0, y: btnsView.center.y + btnsView.frame.height/2 + 16, width: 40, height: 40))
        btnClose.center.x = view.center.x
        btnClose.setTitle("×", for: .normal)
        btnClose.setTitleColor(UIColor.white, for: .normal)
        btnClose.titleLabel?.font = UIFont.systemFont(ofSize: 24)
        bgView.addSubview(btnClose)
        btnClose.addTarget(self, action: #selector(TutorialViewController.close), for: .touchUpInside)
        
        let btn1 = UIButton(frame: CGRect(x: 0, y: 0, width: btnsView.frame.width/2 - 32, height: btnsView.frame.width/2 - 32))
        btn1.center.x = btnsView.frame.width / 4
        btn1.setImage(#imageLiteral(resourceName: "computer"), for: .normal)
        btn1.imageView?.contentMode = .scaleAspectFit
        btnsView.addSubview(btn1)
        
        let title1 = UIButton(frame: CGRect(x: 0, y: btnsView.frame.height/2 - 32, width: btnsView.frame.width/2, height: 32))
        title1.setTitleColor(UIColor.white, for: .normal)
        title1.setTitle("从电脑上传", for: .normal)
        title1.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btnsView.addSubview(title1)
        
        btn1.tag = 1
        title1.tag = 1
        btn1.addTarget(self, action: #selector(TutorialViewController.btnClicked(btn:)), for: .touchUpInside)
        title1.addTarget(self, action: #selector(TutorialViewController.btnClicked(btn:)), for: .touchUpInside)
        
        let btn2 = UIButton(frame: CGRect(x: 0, y: 0, width: btnsView.frame.width/2 - 32, height: btnsView.frame.width/2 - 32))
        btn2.center.x = btnsView.frame.width / 4 * 3
        btn2.setImage(#imageLiteral(resourceName: "email_whitelarge"), for: .normal)
        btn2.imageView?.contentMode = .scaleAspectFit
        btnsView.addSubview(btn2)
        
        let title2 = UIButton(frame: CGRect(x: btnsView.frame.width/2, y: btnsView.frame.height/2 - 32, width: btnsView.frame.width/2, height: 32))
        title2.setTitleColor(UIColor.white, for: .normal)
        title2.setTitle("从邮件上传", for: .normal)
        title2.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btnsView.addSubview(title2)
        
        btn2.tag = 2
        title2.tag = 2
        btn2.addTarget(self, action: #selector(TutorialViewController.btnClicked(btn:)), for: .touchUpInside)
        title2.addTarget(self, action: #selector(TutorialViewController.btnClicked(btn:)), for: .touchUpInside)
        
        let btn3 = UIButton(frame: CGRect(x: 0, y: btnsView.frame.height/2, width: btnsView.frame.width/2 - 32, height: btnsView.frame.width/2 - 32))
        btn3.center.x = btnsView.frame.width / 4
        btn3.setImage(#imageLiteral(resourceName: "qq_whitelarge"), for: .normal)
        btn3.imageView?.contentMode = .scaleAspectFit
        btnsView.addSubview(btn3)
        
        let title3 = UIButton(frame: CGRect(x: 0, y: btnsView.frame.height - 32, width: btnsView.frame.width/2, height: 32))
        title3.setTitleColor(UIColor.white, for: .normal)
        title3.setTitle("从QQ上传", for: .normal)
        title3.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btnsView.addSubview(title3)
        
        btn3.tag = 3
        title3.tag = 3
        btn3.addTarget(self, action: #selector(TutorialViewController.btnClicked(btn:)), for: .touchUpInside)
        title3.addTarget(self, action: #selector(TutorialViewController.btnClicked(btn:)), for: .touchUpInside)
        
        let btn4 = UIButton(frame: CGRect(x: 0, y: btnsView.frame.height/2, width: btnsView.frame.width/2 - 32, height: btnsView.frame.width/2 - 32))
        btn4.center.x = btnsView.frame.width / 4 * 3
        btn4.setImage(#imageLiteral(resourceName: "wechat_whitelarge"), for: .normal)
        btn4.imageView?.contentMode = .scaleAspectFit
        btnsView.addSubview(btn4)
        
        let title4 = UIButton(frame: CGRect(x: btnsView.frame.width/2, y: btnsView.frame.height - 32, width: btnsView.frame.width/2, height: 32))
        title4.setTitleColor(UIColor.white, for: .normal)
        title4.setTitle("从微信上传", for: .normal)
        title4.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btnsView.addSubview(title4)
        
        btn4.tag = 4
        title4.tag = 4
        btn4.addTarget(self, action: #selector(TutorialViewController.btnClicked(btn:)), for: .touchUpInside)
        title4.addTarget(self, action: #selector(TutorialViewController.btnClicked(btn:)), for: .touchUpInside)
        
        imageView = UIButton(frame: view.frame)
        imageView.alpha = 0
        imageView.adjustsImageWhenHighlighted = false
        imageView.addTarget(self, action: #selector(TutorialViewController.closeImage), for: .touchUpInside)
        bgView.addSubview(imageView)
        
        let closeBtn = UILabel(frame: CGRect(x: 8, y: 8, width: 44, height: 44))
//        closeBtn.setTitle("×", for: .normal)
        closeBtn.text = "×"
        closeBtn.font = UIFont.boldSystemFont(ofSize: 36)
        closeBtn.textColor = themeColor2
//        closeBtn.setTitleColor(themeColor2, for: .normal)
        imageView.addSubview(closeBtn)
    }
    
    func close(){
        dismiss(animated: true, completion: nil)
    }
    
    func btnClicked(btn: UIButton){
        print("btn\(btn.tag) clicked")
        let imageFileName = "tu\(btn.tag)"
        
        imageView.setImage(UIImage(named: imageFileName), for: .normal)
        UIView.animate(withDuration: 0.3, animations: {
            self.imageView.alpha = 1
        })
        
//        let imageFilePath = Bundle.main.path(forResource: imageFileName, ofType: "jpg")
//        if imageFilePath != nil {
//            imageView.setImage(UIImage(contentsOfFile: imageFilePath!), for: .normal)
            
//        }
        
        
        
    }
    
    func closeImage(){
        UIView.animate(withDuration: 0.3, animations: {
            self.imageView.alpha = 0
        })
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
